DhabiZone = ZONE:New("DhabiZone")
ZoneCaptureCoalition_AbuDhabi = ZONE_CAPTURE_COALITION:New(DhabiZone,
SIDE.BLUE)

ZoneCaptureCoalition_AbuDhabi:__Guard(1)

ZoneCaptureCoalition_AbuDhabi:Start(1, 5)

-- zoneflag_AbuDhabi: 如果为true则代表之前是由蓝方占领的
local zoneflag_AbuDhabi = true

local airportname = "阿布扎比"

local function clientsController(switch)
    CLIENTS_AbuDhabi:ForEachClient(function ( client )
        local name = client.ClientName
        USERFLAG:New(name):Set(switch)
    end)
end

-- 开始防卫区域
function ZoneCaptureCoalition_AbuDhabi:OnEnterGuarded( From, Event, To )
    
    if From ~= To then
        local coalition = self:GetCoalition()
        if coalition == SIDE.RED then
            MESSAGE:New("敌人占领了" .. airportname .. "机场!", 10, "格里芬战局通报", false):ToAll()
            MSG_MISSION1["task1"] = " ○ 协助友军夺回 阿布扎比机场\n"
            SOUND_RADIOTRANS:ToAll()
            
            clientsController(100) -- 禁用机场客户端

            if RED_BASE_AbuDhabi_01:GetFirstAliveGroup() == nil then
                RED_BASE_AbuDhabi_01:Spawn()
            end -- 如果红方基地被摧毁了，则新建一个

            if RED_AbuDhabi_ATTK_01 then
                RED_AbuDhabi_ATTK_01:SpawnScheduleStop()
            end -- 进攻Abu Dhabi的车队01

            if RED_AbuDhabi_ATTK_02 then
                RED_AbuDhabi_ATTK_02:SpawnScheduleStop()
            end -- 进攻Abu Dhabi的车队02
            
            if RED_AbuDhabi_ATTK_Helo then
                RED_AbuDhabi_ATTK_Helo:SpawnScheduleStop()
            end -- 进攻Abu Dhabi的直升机队

            zoneflag_AbuDhabi = false

        elseif coalition == SIDE.BLUE then
            MESSAGE:New("我们占领了" .. airportname .. "机场。请协助防御该机场。", 10, "格里芬战局通报", false):ToAll()
            MSG_MISSION1["task1"] = " ○ 协助友军防守 阿布扎比机场\n"
            clientsController(0)
            SOUND_AREASECURE:ToAll()
            
            zoneflag_AbuDhabi = true -- 更改Abu Dhabi机场flag

            if BLUE_BASE_AbuDhabi:GetFirstAliveGroup() == nil then -- 生成蓝方基地检查
                BLUE_BASE_AbuDhabi:Spawn()
            end
            
            if RED_AbuDhabi_ATTK_01 then
                RED_AbuDhabi_ATTK_01:SpawnScheduleStart()
            end -- 进攻Abu Dhabi的车队01

            if RED_AbuDhabi_ATTK_02 then
                RED_AbuDhabi_ATTK_02:SpawnScheduleStart()
            end -- 进攻Abu Dhabi的车队02

            if RED_AbuDhabi_ATTK_Helo then
                RED_AbuDhabi_ATTK_Helo:SpawnScheduleStart()
            end -- 进攻Abu Dhabi的直升机队
        end
    end
end

-- enter empty function
function ZoneCaptureCoalition_AbuDhabi:OnEnterEmpty()
    if zoneflag_AbuDhabi == false then -- 如果蓝方清空了机场

        MESSAGE:New("机场威胁已清除，地面部队正在接管" .. airportname .. "机场。", 10, "格里芬战局通报", false):ToAll()
        SOUND_JTFTRANS:ToAll()
        BLUE_BASE_AbuDhabi:Spawn()
    else -- 如果红方清理了机场
        RED_BASE_AbuDhabi_01:Spawn()
    end
end


function ZoneCaptureCoalition_AbuDhabi:OnEnterAttacked(From, Event, To)
    local coalition = self:GetCoalition()
    if coalition == SIDE.BLUE then
        MESSAGE:New(airportname .. "机场正在被敌方攻击！", 10, "格里芬战局通报", false):ToAll()
    else
        MESSAGE:New("我们正在攻击" .. airportname .. "机场！", 10, "格里芬战局通报", false):ToAll()
    end
    SOUND_JTFTRANS:ToAll()
end

